﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace WebUI.LabManage
{
    public partial class Lab : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bnNew.OnClientClick = window.GetShowReference("LabEdit.aspx?action=new", "新增实验室");
                BindGrid();
                BindDL();
            }
            if (Request[Common.LinkService.RefreshTag] == Common.LinkService.RefreshString)
            {
                BindGrid();
            }
        }

        private void BindDL()
        {
            dl_level.DataSource = BLL.Parameter.GetParamList("3");
            dl_level.DataTextField = "value";
            dl_level.DataValueField = "id";
            dl_level.DataBind();
            dl_category.DataSource = BLL.Parameter.GetParamList("4");
            dl_category.DataTextField = "value";
            dl_category.DataValueField = "id";
            dl_category.DataBind();
            dl_location.DataSource = BLL.Parameter.GetParamList("10");
            dl_location.DataTextField = "value";
            dl_location.DataValueField = "id";
            dl_location.DataBind();
            ExtAspNet.ListItem item = new ExtAspNet.ListItem("全部", "-1");
            dl_level.Items.Insert(0,item);
            dl_category.Items.Insert(0, item);
            dl_location.Items.Insert(0, item);
        }

        private void BindGrid()
        {
            Grid1.DataSource = BLL.LabManage.GetLabList();
            Grid1.DataBind();
        }
        protected string GetDelUrl(object id)
        {
            return "javascript:if(confirm('确认删除吗？'))" + window.GetShowReference("LabEdit.aspx?action=del&id=" + id);
        }

        protected void bnSearch_Click(object sender, EventArgs e)
        {
            StringBuilder strWhere = new StringBuilder();
            if (tb_labname.Text.Trim() != "")
            {
                strWhere.AppendFormat(" LabName LIKE '%{0}%'",tb_labname.Text.Trim());
            }
            if (dl_location.SelectedValue != "-1")
            {
                if (tb_labname.Text.Trim() != "")
                {
                    strWhere.Append(" AND");
                }
                strWhere.AppendFormat(" location = {0}", dl_location.SelectedValue);
            }
            if(dl_level.SelectedValue != "-1")
            {
                if (tb_labname.Text.Trim() != "" || dl_location.SelectedValue != "-1")
                {
                    strWhere.Append(" AND");
                }
                strWhere.AppendFormat(" LabLevel = {0}",dl_level.SelectedValue);
            }
            if(dl_category.SelectedValue != "-1")
            {
                if (tb_labname.Text.Trim() != "" || dl_location.SelectedValue != "-1" || dl_level.SelectedValue != "-1")
                {
                    strWhere.Append(" AND");
                }
                strWhere.AppendFormat(" category = {0}", dl_category.SelectedValue);
            }
            Grid1.DataSource = BLL.LabManage.GetLabList(strWhere.ToString());
            Grid1.DataBind();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }

        protected void bnReset_Click(object sender, EventArgs e)
        {
            BindGrid();
            tb_labname.Text = "";
            dl_location.SelectedValue = "-1";
            dl_category.SelectedValue = "-1";
            dl_level.SelectedValue = "-1";
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
                if (BLL.LabManage.DeleteByID(id))
                {
                    BindGrid();
                }
                else
                {
                    Alert.ShowInTop("删除失败！", "错误", MessageBoxIcon.Error);
                }
            }
        }
    }
}